home comics writing pictures archive about

SqlExport.bas

Language: Visual Basic Module
Last Modified: 2020-06-27 1:58:30 PM UTC
File Size: 22420 bytes
http://www.penguinstew.ca/example/ExcelSQLExport/SqlExport.bas
Attribute VB_Name = "SqlExport"
Option Explicit
Sub SqlExport()
Dim months As Vector
Dim catagories As Vector
Dim specialAccounts As Vector
Dim trans As Vector
'Initalize vectors
Set months = New Vector
Set catagories = New Vector
Set specialAccounts = New Vector
Set trans = New Vector
'Setup vector types
months.SetType New month
catagories.SetType New Catagory
specialAccounts.SetType New specialAccount
trans.SetType New Transaction
Dim transSavingCheuqe As Integer
Dim transSavingCash As Integer
Dim transCheuqeSaving As Integer
Dim transCheuqeCash As Integer
Dim transCashSaving As Integer
Dim transCashCheuqe As Integer
Dim cashDebit As Integer
Dim sysCat As Catagory
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
transSavingCheuqe = sysCat.CatagoryId
sysCat.Name = "Transfer Savings to Chequing"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 1
sysCat.FromAccount = 0
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
transSavingCash = sysCat.CatagoryId
sysCat.Name = "Transfer Savings to Cash"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 2
sysCat.FromAccount = 0
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
transCheuqeSaving = sysCat.CatagoryId
sysCat.Name = "Transfer Chequing to Savings"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 0
sysCat.FromAccount = 1
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
transCheuqeCash = sysCat.CatagoryId
sysCat.Name = "Transfer Chequing to Cash"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 2
sysCat.FromAccount = 1
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
transCashSaving = sysCat.CatagoryId
sysCat.Name = "Transfer Cash to Savings"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 0
sysCat.FromAccount = 2
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
transCashCheuqe = sysCat.CatagoryId
sysCat.Name = "Transfer Cash to Chequing"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 1
sysCat.FromAccount = 2
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
sysCat.Name = "Special Account Credit"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 3
sysCat.FromAccount = 4
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
sysCat.Name = "Special Account Debit"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 4
sysCat.FromAccount = 3
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
sysCat.Name = "Special Account Credit from Savings"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 3
sysCat.FromAccount = 0
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
sysCat.Name = "Special Account Debit to Savings"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 0
sysCat.FromAccount = 3
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
sysCat.Name = "Special Account Credit from Chequing"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 3
sysCat.FromAccount = 1
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
sysCat.Name = "Special Account Debit to Chequing"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 1
sysCat.FromAccount = 3
sysCat.InUse = True
sysCat.IsSystem = True
catagories.Add sysCat
Set sysCat = New Catagory
sysCat.CatagoryId = catagories.count + 1
cashDebit = sysCat.CatagoryId
sysCat.Name = "Cash debit"
sysCat.colour = "#bebebe"
sysCat.ToAccount = 4
sysCat.FromAccount = 2
sysCat.InUse = True
sysCat.IsSystem = False
catagories.Add sysCat
Dim Sheet As Worksheet
For Each Sheet In Worksheets()
Dim i As Integer
Dim j As Integer
Dim k As Integer
Sheet.Activate
'Skip the starting "Graph" sheets
If Not Sheet.Name = "Budget" And Not Sheet.Name = "Balance History" And Not Sheet.Name = "Bill History" _
And Not Sheet.Name = "Total History" And Not Sheet.Name = "Expense History" Then
'Clear catagory usage
For j = 0 To catagories.count - 1
If Not catagories.GetAt(j).IsSystem Then
catagories.GetAt(j).InUse = False
End If
Next j
'Get starting account values
Dim startCheuq As Double
Dim startSave As Double
Dim startCash As Double
startCheuq = Cells(1, "G").value
startSave = Cells(1, "H").value
startCash = Cells(1, "I").value
'Add month to vector
Dim month As month
Set month = New month
month.MonthId = months.count + 1
month.MonthDate = Cells(1, "A").value
month.StartCheuqing = round(startCheuq)
month.StartSavings = round(startSave)
month.startCash = round(startCash)
months.Add month
'Setup vector for catagories in this month
Dim localCatagories As Vector
Set localCatagories = New Vector
localCatagories.SetType New Catagory
'Setup vector for special accounts in this month
Dim localSpecialAccounts As Vector
Set localSpecialAccounts = New Vector
localSpecialAccounts.SetType New specialAccount
'Setup catagory/special account type variables
Dim fromAccnt As Integer
Dim toAccnt As Integer
Dim isSpecial As Boolean
fromAccnt = 4
toAccnt = 0
isSpecial = False
'Get the last catagory row
Dim FinalCatagory As Integer
FinalCatagory = Cells(Rows.count, 12).End(xlUp).Row
For i = 1 To FinalCatagory
Dim colour As String
Dim cellValue As String
'Get the colour of the cell and convert to hex string
colour = Hex$(Cells(i, "L").Interior.Color)
'Get the text of the cell
cellValue = Cells(i, "L").value
cellValue = LCase(cellValue)
'Start of expenses
If cellValue = "expensess" Then
fromAccnt = 1
toAccnt = 4
End If
'Savings account expenses
If cellValue = "services" Or cellValue = "goods" Then
fromAccnt = 0
End If
'Start of special accounts
If cellValue = "total" And colour = "0" Then
isSpecial = True
End If
'Skip blank cells, sum rows and cells without colour
If Not cellValue = "" And Not cellValue = "income" And Not cellValue = "expenses" And _
Not cellValue = "total" And Not colour = "FFFFFF" Then
If Not isSpecial Then
Dim found As Boolean
found = False
'See if there's already a catagory with a matching name
If Not catagories.IsEmpty Then
For j = 0 To catagories.count - 1
If catagories.GetAt(j).Name = StrConv(escape(cellValue), vbProperCase) Then
found = True
catagories.GetAt(j).InUse = True
localCatagories.Add catagories.GetAt(j)
Exit For
End If
Next j
End If
'Add a new one if not found
If Not found Then
Dim cat As Catagory
Set cat = New Catagory
cat.CatagoryId = catagories.count + 1
cat.Name = StrConv(escape(cellValue), vbProperCase)
cat.colour = GetRgbColour(colour)
cat.FromAccount = fromAccnt
cat.ToAccount = toAccnt
cat.InUse = True
catagories.Add cat
localCatagories.Add cat
End If
Else
Dim ammount As Double
ammount = Cells(i, "N").value
'Use the second cell to get the list of related transactions
Dim specTrans As String
Dim specArray() As String
specTrans = Cells(i, "M").Formula
specTrans = Replace(specTrans, "=", "")
specTrans = Replace(specTrans, "C", "")
specTrans = Replace(specTrans, "E", "")
specArray = Split(specTrans, "+")
Dim spec As specialAccount
Set spec = New specialAccount
spec.SpecialAccountMonthId = specialAccounts.count + 1
spec.MonthId = month.MonthId
spec.Name = escape(cellValue)
spec.amount = round(ammount)
spec.isNew = True
Dim highestAcntId As Integer
found = False
highestAcntId = 0
If Not specialAccounts.IsEmpty Then
For j = 0 To specialAccounts.count - 1
If specialAccounts.GetAt(j).SpecialAccountId >= highestAcntId Then
highestAcntId = specialAccounts.GetAt(j).SpecialAccountId + 1
End If
If specialAccounts.GetAt(j).Name = spec.Name Then
found = True
spec.isNew = False
spec.SpecialAccountId = specialAccounts.GetAt(j).SpecialAccountId
Exit For
End If
Next j
End If
If Not found Then
spec.SpecialAccountId = highestAcntId
End If
Dim specRow As Variant
For Each specRow In specArray
spec.rowNumbers.Add (CInt(specRow))
Next specRow
specialAccounts.Add spec
localSpecialAccounts.Add spec
End If
End If
If cellValue = "services" Or cellValue = "goods" Then
fromAccnt = 1
End If
Next i
Dim FinalTransaction As Integer
FinalTransaction = Cells(Rows.count, 7).End(xlUp).Row
Dim currentDate As Date
For i = 3 To FinalTransaction
If Not Cells(i, "A").value = "" Then
currentDate = GetCorrectDate(Cells(i, "A").value)
End If
Dim cheuqeFormula As String
Dim saveFormula As String
Dim cashFormula As String
cheuqeFormula = Cells(i, "G").Formula
saveFormula = Cells(i, "H").Formula
cashFormula = Cells(i, "I").Formula
Dim toCheuqe As Boolean
Dim fromCheuqe As Boolean
Dim toSave As Boolean
Dim fromSave As Boolean
Dim toCash As Boolean
Dim fromCash As Boolean
toCheuqe = Not InStr(cheuqeFormula, "+") = 0
fromCheuqe = Not InStr(cheuqeFormula, "-") = 0
toSave = Not InStr(saveFormula, "+") = 0
fromSave = Not InStr(saveFormula, "-") = 0
toCash = Not InStr(cashFormula, "+") = 0
fromCash = Not InStr(cashFormula, "-") = 0
Dim IsCash As Boolean
IsCash = toCash Or fromCash
Dim valueColumn As String
Dim ammountColumn As String
If Not Cells(i, "B").value = "" Then
valueColumn = "B"
ammountColumn = "C"
ElseIf Not Cells(i, "D").value = "" Then
valueColumn = "D"
ammountColumn = "E"
End If
If Not valueColumn = "" Then
colour = Hex$(Cells(i, valueColumn).Interior.Color)
cellValue = Cells(i, valueColumn).value
ammount = Cells(i, ammountColumn).value
Dim Transaction As Transaction
If Not colour = "FFFFFF" Then
colour = GetRgbColour(colour)
Set Transaction = New Transaction
Transaction.MonthId = month.MonthId
Transaction.TransDate = currentDate
Transaction.Description = escape(cellValue)
Transaction.amount = round(ammount)
Transaction.CatagoryId = 0
Transaction.RecurringExpenseId = 0
If IsCash Then
'Cash transaction
Transaction.CatagoryId = cashDebit
Else
For j = 0 To localCatagories.count - 1
If localCatagories.GetAt(j).colour = colour Then
Transaction.CatagoryId = localCatagories.GetAt(j).CatagoryId
Exit For
End If
Next j
End If
Transaction.SpecialAccountId = 0
For j = 0 To localSpecialAccounts.count - 1
For k = 0 To localSpecialAccounts.GetAt(j).rowNumbers.count - 1
If i = localSpecialAccounts.GetAt(j).rowNumbers.GetAt(k) Then
Transaction.SpecialAccountId = localSpecialAccounts.GetAt(j).SpecialAccountMonthId
End If
Next k
Next j
If Transaction.CatagoryId = 0 Then
MsgBox "Transaction '" & Transaction.Description & "' has no catagory, skipping"
Else
trans.Add Transaction
End If
ElseIf Not Cells(i, "B").value = "" And Not Cells(i, "D").value = "" Then
'transfer
Set Transaction = New Transaction
Transaction.MonthId = month.MonthId
Transaction.TransDate = currentDate
Transaction.Description = escape(cellValue)
Transaction.amount = round(ammount)
Transaction.RecurringExpenseId = 0
Transaction.SpecialAccountId = 0
If fromSave And toCheuqe Then
Transaction.CatagoryId = transSavingCheuqe
ElseIf fromSave And toCash Then
Transaction.CatagoryId = transSavingCash
ElseIf fromCheuqe And toSave Then
Transaction.CatagoryId = transCheuqeSaving
ElseIf fromCheuqe And toCash Then
Transaction.CatagoryId = transCheuqeCash
ElseIf fromCash And toSave Then
Transaction.CatagoryId = transCashSaving
ElseIf fromCash And toCheuqe Then
Transaction.CatagoryId = transCashCheuqe
End If
If Transaction.CatagoryId = 0 Then
MsgBox "Transaction '" & Transaction.Description & "' has no catagory, skipping"
Else
trans.Add Transaction
End If
End If
End If
Next i
End If
Next Sheet
outputSql months, catagories, specialAccounts, trans
MsgBox "Done!"
End Sub
Sub outputSql(ByRef months As Vector, ByRef catagories As Vector, ByRef specialAccounts As Vector, ByRef trans As Vector)
Dim file As String
Dim i As Integer
file = ThisWorkbook.Path & "\sqlExport.sql"
On Error Resume Next
Kill file
On Error GoTo 0
Open file For Output As #1
Print #1, "DELETE FROM [Transactions]"
Print #1, "DBCC CHECKIDENT([Transactions], RESEED, 1)"
Print #1, "DELETE FROM [BudgetCategories]"
Print #1, "DBCC CHECKIDENT([BudgetCategories], RESEED, 1)"
Print #1, "DELETE FROM [BudgetMonths]"
Print #1, "DBCC CHECKIDENT([BudgetMonths], RESEED, 1)"
Print #1, "DELETE FROM [Categories]"
Print #1, "DBCC CHECKIDENT([Categories], RESEED, 1)"
Print #1, "DELETE FROM [SpecialAccountMonths]"
Print #1, "DBCC CHECKIDENT([SpecialAccountMonths], RESEED, 1)"
Print #1, "DELETE FROM [SpecialAccounts]"
Print #1, "DBCC CHECKIDENT([SpecialAccounts], RESEED, 1)"
Print #1, "DELETE FROM [Months]"
Print #1, "DBCC CHECKIDENT([Months], RESEED, 1)"
Print #1, "DELETE FROM [BudgetTypes]"
Print #1, "DBCC CHECKIDENT([BudgetTypes], RESEED, 1)"
Print #1, ""
If Not months.IsEmpty Then
Print #1, "SET IDENTITY_INSERT [Months] ON"
For i = 0 To months.count - 1
Print #1, months.GetAt(i).ToSQL
Next i
Print #1, "SET IDENTITY_INSERT [Months] OFF"
Print #1, ""
End If
If Not catagories.IsEmpty Then
Print #1, "SET IDENTITY_INSERT [Categories] ON"
For i = 0 To catagories.count - 1
Print #1, catagories.GetAt(i).ToSQL
Next i
Print #1, "SET IDENTITY_INSERT [Categories] OFF"
Print #1, ""
End If
If Not specialAccounts.IsEmpty Then
Print #1, "SET IDENTITY_INSERT [SpecialAccounts] ON"
For i = 0 To specialAccounts.count - 1
If specialAccounts.GetAt(i).isNew Then
Print #1, specialAccounts.GetAt(i).ToSQL
End If
Next i
Print #1, "SET IDENTITY_INSERT [SpecialAccounts] OFF"
Print #1, ""
End If
If Not specialAccounts.IsEmpty Then
Print #1, "SET IDENTITY_INSERT [SpecialAccountMonths] ON"
For i = 0 To specialAccounts.count - 1
Print #1, specialAccounts.GetAt(i).ToMonthSql
Next i
Print #1, "SET IDENTITY_INSERT [SpecialAccountMonths] OFF"
Print #1, ""
End If
If Not trans.IsEmpty Then
For i = 0 To trans.count - 1
Print #1, trans.GetAt(i).ToSQL
Next i
End If
Close #1
End Sub
Function GetCorrectDate(dateValue As String) As Date
Dim tempDate As Date
Dim tempDay As Integer
Dim tempMonth As Integer
Dim tempYear As Integer
tempDate = dateValue
If Day(tempDate) <= 12 Then
tempDay = Day(tempDate)
tempMonth = month(tempDate)
tempYear = Year(tempDate)
tempDate = tempMonth & "/" & tempDay & "/" & tempYear
End If
GetCorrectDate = tempDate
End Function
Function GetRgbColour(bgrColour As String) As String
Dim rgbColour As String
Dim j As Integer
Dim colourLeft As String
Dim colourMid As String
Dim colourRight As String
For j = Len(bgrColour) To 5
bgrColour = "0" & bgrColour
Next j
colourLeft = Left(bgrColour, 2)
colourMid = Mid(bgrColour, 3, 2)
colourRight = Right(bgrColour, 2)
GetRgbColour = "#" & colourRight & colourMid & colourLeft
End Function
Function escape(line As String) As String
escape = Replace(line, "'", "''")
End Function
Function round(amount As Double) As Double
round = Format(amount, "#.00")
End Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630